/**
 * Licensed to the Apache Software Foundation （ASF） under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * （the "License"）； you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * https://www.q3z3.com
 * QQ : 939313737
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.platform.modules.sys.controller;

import cn.hutool.core.date.DateUtil;
import com.platform.common.aspectj.SubmitRepeat;
import com.platform.common.web.controller.BaseController;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.sys.domain.SysMenu;
import com.platform.modules.sys.service.SysMenuService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 系统菜单表 控制层
 * </p>
 */
@Controller
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

    @Resource
    private SysMenuService sysMenuService;

    /**
     * 去列表页面
     */
    @RequiresPermissions(value = {"sys:menu:query"})
    @GetMapping(value = "/toList")
    public String toList() {
        return "sys/menu/list";
    }

    /**
     * 列表数据
     */
    @RequiresPermissions(value = {"sys:menu:query"})
    @GetMapping(value = "/list")
    @ResponseBody
    public AjaxResult list(SysMenu sysMenu) {
        orderBy("sort");
        List<SysMenu> list = sysMenuService.queryList(sysMenu);
        return AjaxResult.success(list);
    }

    /**
     * 菜单新增/修改-树列表
     */
    @GetMapping(value = "/tree/{pid}")
    @ResponseBody
    public AjaxResult tree(@PathVariable("pid") Long pid) {
        return AjaxResult.success(sysMenuService.tree(pid));
    }

    /**
     * 左侧菜单列表
     */
    @GetMapping(value = "/menuTree")
    @ResponseBody
    public AjaxResult menuTree() {
        return AjaxResult.success(sysMenuService.menuTree());
    }

    /**
     * 角色下拉树
     */
    @RequiresPermissions(value = {"sys:role:add", "sys:role:edit"}, logical = Logical.OR)
    @GetMapping(value = "/roleMenu/{roleId}")
    @ResponseBody
    public AjaxResult roleMenu(@PathVariable("roleId") Long roleId) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", sysMenuService.queryList(new SysMenu()));
        if (roleId != 0L) {
            map.put("menuIds", sysMenuService.queryByRoleId(roleId));
        }
        return AjaxResult.success(map);
    }

    /**
     * 新增页面
     */
    @RequiresPermissions(value = {"sys:menu:add"})
    @GetMapping(value = "/toAdd/{pid}")
    public String toAdd(@PathVariable String pid, Model model) {
        model.addAttribute("pid", pid);
        return "sys/menu/add";
    }

    /**
     * 新增数据
     */
    @RequiresPermissions(value = {"sys:menu:add"})
    @PostMapping(value = "/add")
    @ResponseBody
    @SubmitRepeat
    public AjaxResult add(SysMenu sysMenu) {
        sysMenu.setId(DateUtil.currentSeconds());
        sysMenuService.add(sysMenu);
        return AjaxResult.successMsg("新增成功");
    }

    /**
     * 修改页面
     */
    @RequiresPermissions(value = {"sys:menu:edit"})
    @GetMapping(value = "/toEdit")
    public String toEdit(Model model, Long id) {
        model.addAttribute("entity", sysMenuService.getById(id));
        return "sys/menu/edit";
    }

    /**
     * 修改数据
     */
    @RequiresPermissions(value = {"sys:menu:edit"})
    @PostMapping(value = "/edit")
    @ResponseBody
    public AjaxResult edit(SysMenu sysMenu) {
        if (sysMenu.getId().equals(sysMenu.getPid())) {
            sysMenu.setPid(0L);
        }
        sysMenuService.updateById(sysMenu);
        return AjaxResult.successMsg("修改成功");
    }

    /**
     * 删除数据
     */
    @RequiresPermissions(value = {"sys:menu:delete"})
    @GetMapping(value = "/delete")
    @ResponseBody
    public AjaxResult delete(SysMenu sysMenu) {
        sysMenuService.deleteMenu(sysMenu.getId());
        return AjaxResult.successMsg("删除成功");
    }

    /**
     * 拷贝菜单
     */
    @RequiresPermissions(value = {"sys:menu:copy"})
    @GetMapping(value = "/copy")
    @ResponseBody
    public AjaxResult copy(SysMenu sysMenu) {
        sysMenuService.copyMenu(sysMenu.getId());
        return AjaxResult.successMsg("拷贝成功");
    }

}

